package sit.engreal.leetcode.problems100;


import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.*;

/**
 * <a href="https://leetcode.cn/problems/two-sum/"> 1. 两数之和 </a>
 */
public class LeetCode1 {
    public static void main(String[] args) {
        class Solution {
            public int[] twoSum(int[] nums, int target) {
                final HashMap<Integer, Integer> map = new HashMap<>();
                for (int i = 0; i < nums.length; i++) {
                    if (map.containsKey(nums[i])) {
                        return new int[]{map.get(nums[i]), i};
                    } else {
                        map.put(target - nums[i], i);
                    }
                }

                return new int[]{-1, -1};
            }
        }

        final Solution solution = new Solution();

        assertArrayEquals(new int[]{0, 1}, solution.twoSum(new int[]{2, 7, 11, 15}, 9));
        assertArrayEquals(new int[]{1, 2}, solution.twoSum(new int[]{3, 2, 4}, 6));
        assertArrayEquals(new int[]{0, 1}, solution.twoSum(new int[]{3, 3}, 6));
    }
}
